.TH std::list::back 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::list::back \- std::list::back

.SH Synopsis
   reference back();             \fB(1)\fP
   const_reference back() const; \fB(2)\fP

   Returns a reference to the last element in the container.

   Calling back on an empty container causes undefined behavior.

.SH Parameters

   \fI(none)\fP

.SH Return value

   Reference to the last element.

.SH Complexity

   Constant.

.SH Notes

   For a non-empty container c, the expression c.back() is equivalent to
   *std::prev(c.end()).

.SH Example

   The following code uses back to display the last element of a std::list<char>:


// Run this code

 #include <list>
 #include <iostream>

 int main()
 {
     std::list<char> letters{'a', 'b', 'c', 'd', 'e', 'f'};

     if (!letters.empty())
         std::cout << "The last character is '" << letters.back() << "'.\\n";
 }

.SH Output:

 The last character is 'f'.

.SH See also

   front access the first element
         \fI(public member function)\fP
